import { Component, OnInit } from '@angular/core';
import { CATALOGUES } from './model/catalogues';
import { Catalogue } from './model/catalogues-model';
import { catalogueService } from './model/catalogueService/catalogueService';
import { Router } from '@angular/router';

@Component({
    selector: 'bookCatalogue',
    templateUrl: 'book-catalogue.component.html',
    styleUrls: ['book-catalogue.component.scss']
})

export class BookCatalogueComponent implements OnInit {
    title: string = "Tour of Library";
    selectedCatalogue: Catalogue;
    bookcatalogues: Catalogue[];
    constructor(private bookcat: catalogueService,
        private router: Router) {
    }

    ngOnInit() {
        this.bookcat.getCatalogues()
            .then(bookcatalogues => this.bookcatalogues = bookcatalogues);
        // this.bookcatalogues = this.bookcat.getCatalogues();

    }

    onSelect(catalogue: Catalogue) {
        this.selectedCatalogue = catalogue;
    }

    gotoDetail() {
        this.router.navigate(['/detail', this.selectedCatalogue.id]);
    }

    add(id: any, name: string) {
        this.bookcat.add(id, name);
        this.bookcatalogues = CATALOGUES;
        // let temp = this.catalogueSearch.search("算术");
    }

    delete(catalogue: Catalogue) {
        this.bookcat.delete(catalogue);
        this.bookcatalogues = this.bookcatalogues.filter(h => h !== catalogue);
        if (this.selectedCatalogue === catalogue) {
            this.selectedCatalogue = null;
        }
    }
}